<!DOCTYPE html>
<html lang="en">

<head>
	<title>three.js webgl - buffer geometry custom attributes - particles</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<style>
		body {
				color: #ffffff;
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				position: absolute;
				top: 0px;
				width: 100%;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				text-align: center;
				font-weight: bold;
			}
			a {
				color: #fff;
			}
		</style>
</head>

<body>
	<div id="container"></div>
	<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry custom
		attributes - particles</div>

	<script src="../build/three.js"></script>

	<script src="js/WebGL.js"></script>
	<script src="js/libs/stats.min.js"></script>

	<script type="x-shader/x-vertex" id="vertexshader">

			attribute float size;
			uniform float heightOfNearPlane;

			void main() {

				gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

				gl_PointSize = size / gl_Position.w * heightOfNearPlane;

			}
		</script>

	<script type="x-shader/x-fragment" id="fragmentshader">

			uniform sampler2D texture;

			void main() {

				vec4 outColor = texture2D( texture, gl_PointCoord );
				vec4 vColor = vec4( 0.0, 1.0, 0.0, 1.0);

				if ( outColor.a < 0.5 ) discard;

				gl_FragColor = outColor * vec4( vColor.xyz, 1.0 );

				float depth = gl_FragCoord.z / gl_FragCoord.w;
				const vec3 fogColor = vec3( 0.0 );

				float fogFactor = smoothstep( 200.0, 600.0, depth );
				gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );

			}
		</script>


	<script>
		var scale = 5.0;
		var data = {
			'positions': [
				0, 0, 0,
				5, 0, 0,
				0, 3, 0
			],
			'sizes': [2, 3, 0.5]
		}

		if (WEBGL.isWebGLAvailable() === false) {

			document.body.appendChild(WEBGL.getWebGLErrorMessage());

		}

		var renderer, scene, camera, stats;

		var particleSystem, geometry;

		camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
		console.log(window.innerHeight);

		var heightOfNearPlaneV = window.innerHeight/(2*Math.tan(75*3.1415926/2/180));
		console.log(heightOfNearPlaneV)

		var texture = new THREE.TextureLoader().load( "textures/sprites/ball.png" );
		texture.wrapS = THREE.RepeatWrapping;
		texture.wrapT = THREE.RepeatWrapping;

		camera.position.z = 10;

		scene = new THREE.Scene();

		var shaderMaterial = new THREE.ShaderMaterial({

			uniforms: {
				heightOfNearPlane: {value: heightOfNearPlaneV},
				texture: { value: texture }
			},
			vertexShader: document.getElementById('vertexshader').textContent,
			fragmentShader: document.getElementById('fragmentshader').textContent,

			blending: THREE.AdditiveBlending,
			depthTest: false,
			transparent: true,
			vertexColors: true

		});

		geometry = new THREE.BufferGeometry();

		var positions = data['positions'];
		for (var i=0;i<positions.length;++i){
			positions[i]/=scale;
		}
		console.log(positions);
		var sizes = data['sizes'];

		geometry.addAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
		geometry.addAttribute('size', new THREE.Float32BufferAttribute(sizes, 1));

		particleSystem = new THREE.Points(geometry, shaderMaterial);

		scene.add(particleSystem);

		renderer = new THREE.WebGLRenderer();
		renderer.setPixelRatio(window.devicePixelRatio);
		renderer.setSize(window.innerWidth, window.innerHeight);

		var container = document.getElementById('container');
		container.appendChild(renderer.domElement);

		stats = new Stats();
		container.appendChild(stats.dom);

		window.addEventListener('resize', onWindowResize, false);

		//animation
		animate();

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize(window.innerWidth, window.innerHeight);

		}

		function animate() {

			requestAnimationFrame(animate);

			var time = Date.now() * 0.005;

			particleSystem.rotation.z = 0.01 * time;

			renderer.render(scene, camera);

			stats.update();

		}

	</script>

</body>

</html>